<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 自动投标设置
 * @author chenhuan
 *
 */
class Borrowauto extends MY_Controller {
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		$user = $this->session->userdata('zjy_user');
		if(!isset($user['user_id'])){
			$str = $_SERVER['REQUEST_URI'];
			$this->js->go('/login.html');
		}elseif(!$user['user_id']){
			$this->js->go('/login.html');
		}
		
		$this->tab = 'user';
		$this->sub = 'tender';
	}
	
	/**
	 * 发标
	 * @param $ftype 标类型
	 */
	function add_view(){
		$this->load->helper(array('form'));
		$this->load->model('Borrow_model');
		
		$data = $this->get_user_info('borrowauto_add','alist');
		
		$data['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//借款期限
		$data['borrow_day_time_limit'] 	= $this->Borrow_model->arrays('borrow_day_time_limit');//借款期限
		$data['borrow_style'] 			= $this->Borrow_model->arrays('borrow_style');//还款方式
		$data['title'] 					= "自动投标设置".' - '.CON_KEYWORDS;
        $data['tab_first'] = 'invest';
        $data['tab_second'] = 'invest_auto';
		
		$data['valid_start_time'] = date('Y-m-d',time());
		$data['valid_end_time'] = date('Y-m-d',time()+2*3600*24);
		
		$this->load->view('user/user_borrow_auto_add', $data);
	}
	
	/**
	 * 发标
	 */
	function add(){
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		$fstatus 				= $this->input->post("fstatus");//是否启用
		$tender_type 			= $this->input->post("tender_type");//投标金额类型
		$tender_account 		= $this->input->post("tender_account");//按金额，起始金额
		$tender_account1 		= $this->input->post("tender_account1");//按金额，最大金额
		//$tender_scale 			= $this->input->post("tender_scale");//按比例
		$borrow_style_status	= $this->input->post("borrow_style_status");//是否启用还款方式
		$borrow_style 			= $this->input->post("borrow_style");//是否选择还款方式
		$timelimit_status		= $this->input->post("timelimit_status");//是否期限
		$timelimit_month_first	= $this->input->post("timelimit_month_first");//投标起始期限
		$timelimit_month_last	= $this->input->post("timelimit_month_last");//投标结束期限
		$apr_status 			= $this->input->post("apr_status");//是否选择利率
		$apr_first 				= $this->input->post("apr_first");//起始利率
		$apr_last 				= $this->input->post("apr_last");//结束利率
		$award_status 			= $this->input->post("award_status");//是否选择奖励
		$award_first 			= $this->input->post("award_first");//起始奖励
		$award_last 			= $this->input->post("award_last");//结束奖励
		
		$valid_start_time_post = $this->input->post("valid_start_time");
		$valid_end_time_post = $this->input->post("valid_end_time");
		$valid_start_time 			= strtotime($valid_start_time_post.' 00:00:00');//有效开始时间
		$valid_end_time 			= strtotime($valid_end_time_post.' 23:59:59');//有效结束时间
		
		
		if($tender_type == 1){
			if($tender_account < 100 || $tender_account1 < 100 || $tender_account < $tender_account1){
				$ret['msg'] = '投标金额不正确';	
			}
		}elseif($tender_type == 2){
			if($tender_scale < 1){
				$ret['msg'] = '投标比例不正确';	
			}
		}
		
		if($valid_start_time > $valid_end_time){
			$ret['msg'] = '开始时间不能大于结束时间';	
		}
		
		if($ret['msg']){
			echo json_encode($ret);exit;
		}
  		
		$data['fstatus'] 			= $fstatus;
		$data['tender_type'] 		= $tender_type;
		$data['tender_account'] 	= $tender_account;
		$data['tender_account1'] 	= $tender_account1;
		//$data['tender_scale'] 		= $tender_scale;
		$data['borrow_style_status']= $borrow_style_status;
		$data['borrow_style'] 		= $borrow_style;
		$data['timelimit_status'] 	= $timelimit_status;
		$data['timelimit_month_first']= $timelimit_month_first;
		$data['timelimit_month_last']= $timelimit_month_last;
		$data['apr_status'] 		= $apr_status;
		$data['apr_first'] 			= $apr_first;
		$data['apr_last'] 			= $apr_last;
		$data['award_status'] 		= $award_status;
		$data['award_first'] 		= $award_first;
		$data['award_last'] 		= $award_last;
		$data['add_time'] 			= time();
		$data['valid_start_time'] 			= $valid_start_time;
		$data['valid_end_time'] 			= $valid_end_time;
		
		$user = $this->session->userdata('zjy_user');
		$data['user_id']			= $user['user_id'];
		
		$this->load->model('Borrow_model');
		$this->Borrow_model->add_borrow_auto($data);
		$ret['url'] = '/borrowauto/alist/index.html';
		echo json_encode($ret);
	}
	
	
	/**
	 * 发标
	 * @param $ftype 标类型
	 */
	function alist(){
		$data = array();
		$user = $this->session->userdata('zjy_user');
		
		$this->load->model("User_model");
		$user1 = $this->User_model->get_by_uid($user['user_id']);
		
		
		$data['where']['user_id']			= $user['user_id'];
		$data['select'] = 1;
		$this->load->model('Borrow_model');
		$vdata = $this->Borrow_model->get_borrow_auto_list($data);
		

		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标种
		$vdata['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//借款期限
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['num'] = $this->Borrow_model->get_count_auto_tender();

		$userdata = $this->get_user_info('borrowauto_alist','alist');
		
		$vdata['user'] = $userdata['user'];
		$vdata['user_vip'] = $userdata['user_vip'];
		
		$vdata['title'] 						= "自动投标设置列表".' - '.CON_KEYWORDS; 
		$vdata['tab'] = $userdata['tab'];
		$vdata['sub'] = $userdata['sub'];
		$vdata['user_tab'] = $userdata['user_tab'];
		$vdata['user_sub'] = $userdata['user_sub'];
        $vdata['tab_first'] = 'invest';
        $vdata['tab_second'] = 'invest_auto';
		
		$this->load->view('user/user_borrow_auto_list', $vdata);
	}

	function remove($id){
		$user = $this->session->userdata('zjy_user');
		$data['user_id']			= $user['user_id'];
		$data['id']					= $id;
		$this->load->model('Borrow_model');
		$this->Borrow_model->delete_borrow_auto($data);
		header("location:/borrowauto/alist/index.html");
		
	}
	
	function edit_view($id){
		$user = $this->session->userdata('zjy_user');
		$data['user_id']			= $user['user_id'];
		$this->load->model('Borrow_model');
		$vdata = $this->get_user_info('borrowauto_add','alist');
		$borrow_auto_obj = $this->Borrow_model->get_borrow_auto_obj($id, $data);
		$vdata['borrow_auto_obj'] = $borrow_auto_obj;
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//借款期限
		$vdata['borrow_day_time_limit'] 	= $this->Borrow_model->arrays('borrow_day_time_limit');//借款期限
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
        $vdata['tab_first'] = 'invest';
        $vdata['tab_second'] = 'invest_auto';
		$this->load->view('user/user_borrow_auto_edit', $vdata);
	}
	
	/**
	 * 发标
	 */
	function edit(){
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		$id 					= $this->input->post("id");//是否启用
		$fstatus 				= $this->input->post("fstatus");//是否启用
		$tender_type 			= $this->input->post("tender_type");//投标金额类型
		$tender_account 		= $this->input->post("tender_account");//按金额，起始金额
		$tender_account1 		= $this->input->post("tender_account1");//按金额，最大金额
		//$tender_scale 			= $this->input->post("tender_scale");//按比例
		$borrow_style_status	= $this->input->post("borrow_style_status");//是否启用还款方式
		$borrow_style 			= $this->input->post("borrow_style");//是否选择还款方式
		$timelimit_status		= $this->input->post("timelimit_status");//是否期限
		$timelimit_month_first	= $this->input->post("timelimit_month_first");//投标起始期限
		$timelimit_month_last	= $this->input->post("timelimit_month_last");//投标结束期限
		$apr_status 			= $this->input->post("apr_status");//是否选择利率
		$apr_first 				= $this->input->post("apr_first");//起始利率
		$apr_last 				= $this->input->post("apr_last");//结束利率
		$award_status 			= $this->input->post("award_status");//是否选择奖励
		$award_first 			= $this->input->post("award_first");//起始奖励
		$award_last 			= $this->input->post("award_last");//结束奖励
		
		$valid_start_time_post = $this->input->post("valid_start_time");
		$valid_end_time_post = $this->input->post("valid_end_time");
		$valid_start_time 			= strtotime($valid_start_time_post.' 00:00:00');//有效开始时间
		$valid_end_time 			= strtotime($valid_end_time_post.' 23:59:59');//有效结束时间
		
		if($tender_type == 1){
			if($tender_account < 100 || $tender_account1 < 100 || $tender_account < $tender_account1){
				$ret['msg'] = '投标金额不正确';	
			}
		}elseif($tender_type == 2){
			if($tender_scale < 1){
				$ret['msg'] = '投标比例不正确';	
			}
		}
		
		if($valid_start_time > $valid_end_time){
			$ret['msg'] = '开始时间不能大于结束时间';	
		}
		
		if($ret['msg']){
			echo json_encode($ret);exit;
		}
  		
		$data['fstatus'] 			= $fstatus;
		$data['tender_type'] 		= $tender_type;
		$data['tender_account'] 	= $tender_account;
		$data['tender_account1'] 	= $tender_account1;
		//$data['tender_scale'] 		= $tender_scale;
		$data['borrow_style_status']= $borrow_style_status;
		$data['borrow_style'] 		= $borrow_style;
		$data['timelimit_status'] 	= $timelimit_status;
		$data['timelimit_month_first']= $timelimit_month_first;
		$data['timelimit_month_last']= $timelimit_month_last;
		$data['apr_status'] 		= $apr_status;
		$data['apr_first'] 			= $apr_first;
		$data['apr_last'] 			= $apr_last;
		$data['award_status'] 		= $award_status;
		$data['award_first'] 		= $award_first;
		$data['award_last'] 		= $award_last;
		$data['add_time'] 			= time();
		$data['valid_start_time'] 			= $valid_start_time;
		$data['valid_end_time'] 			= $valid_end_time;
		
		$user = $this->session->userdata('zjy_user');
		$data['user_id']			= $user['user_id'];
		
		$this->load->model('Borrow_model');
		$this->Borrow_model->update_borrow_auto($id, $data);
		$ret['url'] = '/borrowauto/alist/index.html';
		echo json_encode($ret);
	}
	
}